package net.gdface.service.facedb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.logging.Slf4JLoggerFactory;


/**
 * 启动 FaceDb thrift 服务
 * @author guyadong
 *
 */
public class FacedbServiceMain implements FacedbServiceConstants{
	private static final FacedbServiceConfig serviceConfig = FacedbServiceConfig.getInstance();

	public FacedbServiceMain() {
	}
	private static void waitquit(){
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		try {
			if (System.console() != null) {  // 检查是否在终端运行
				System.out.println("PRESS 'quit' to exit");
				String line;
				while ((line = reader.readLine()) != null) {
					if ("quit".equalsIgnoreCase(line)) {
						System.exit(0);
					}
				}
			} else {  // 后台运行：改用低 CPU 轮询
				while (true) {
					Thread.sleep(1000);  // 避免空转
				}
			}
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
	}
	public static void main(String ...args){
		Logo.textLogo();
		serviceConfig.parseCommandLine(args);
		// 设置slf4j记录日志,否则会有警告
		InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
		FacedbService.buildService().startAsync();
		if(serviceConfig.isXhrStart()){
			FacedbService.buildHttpService().startAsync();
		}
		if(serviceConfig.isRestfulStart()){
			FacedbService.startRestfulService();
		}
		waitquit();
	}
	
}
